import datetime
import json

from students.serializers import ClassSerializer

from .models import *
from rest_framework import  serializers
class TeachingTimeSerializer(serializers.ModelSerializer):
    def update(self, instance, validated_data):
        obj=super(TeachingTimeSerializer, self).update(instance, validated_data)
        if obj.is_start==True:
            TeachingTime.objects.exclude(id=obj.id).update(is_start=False)
        return obj
    class Meta:
        model=TeachingTime
        fields='__all__'
# 学院课程阶段
class CollegeStageSerializer(serializers.ModelSerializer):
    college_id=serializers.IntegerField(write_only=True)
    college=serializers.StringRelatedField()
    class Meta:
        model = CollegeStage
        fields = '__all__'
class CoursesSerializer(serializers.ModelSerializer):
    school=serializers.StringRelatedField()
    school_id=serializers.IntegerField(write_only=True)
    college_stage=serializers.StringRelatedField()
    college_stage_id=serializers.IntegerField(write_only=True)
    class Meta:
        model=Courses
        fields='__all__'
#主要是用于创建排课信息
class CoursePlanSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        instance=super(CoursePlanSerializer, self).create(validated_data)
        # #生成课表
        teachingTime=validated_data['teaching_time']#教学周期对象
        start=teachingTime.starttime.strftime('%Y-%m-%d')
        end=teachingTime.endtime.strftime('%Y-%m-%d')
        datestart = datetime.datetime.strptime(start, '%Y-%m-%d')
        dateend = datetime.datetime.strptime(end, '%Y-%m-%d')
        morning=json.loads(teachingTime.morning_class_date)
        afternoon=json.loads(teachingTime.afternoon_class_date)
        noclass=json.loads(teachingTime.no_class_date)
        rcList=[]
        while datestart <= dateend:
            cs = validated_data['class_time'].split(',')
            #判断是上午上课还是下午上课
            if datestart.strftime('%Y-%m-%d') in morning:
                cs=cs[0:2]
            if datestart.strftime('%Y-%m-%d') in afternoon:
                cs=cs[2:4]
            if datestart.strftime('%Y-%m-%d') in noclass:
                cs=[]
            for s in cs:
                rc = RollCall(teaching_time=instance.teaching_time,course_plan=instance,lecturer=instance.lecturer.name,lecturer_id=instance.lecturer.id,counsellor=instance.counsellor.name, course=f"{instance.course.name}({instance.course.stage})",class_name=instance.cls.name,class_id=instance.cls.id,class_date=datestart,class_section=s,actual_date=datestart,actual_section=s,college_id=instance.cls.school_id,college_stage_id=instance.course.college_stage_id)
                rcList.append(rc)
            datestart += datetime.timedelta(days=1)
        RollCall.objects.bulk_create(rcList)
        return  instance
    class Meta:
        model=CoursePlan
        fields='__all__'
#主要是 查看排课信息
class UserCoursePlanSerializer(serializers.ModelSerializer):
    id=serializers.IntegerField()
    lecturer = serializers.StringRelatedField(read_only=True)
    counsellor = serializers.StringRelatedField(read_only=True)
    room_number = serializers.CharField(max_length=30)
    class_time = models.CharField(max_length=20)
    course =CoursesSerializer()
    cls = serializers.StringRelatedField(read_only=True)
    cls_id=serializers.IntegerField()
    teaching_time=serializers.StringRelatedField()
    class Meta:
        model=CoursePlan
        fields='__all__'

class AdjustClassMapSerializer(serializers.ModelSerializer):
    class Meta:
        model=AdjustClassMap
        fields=('initial','final')
class AdjustClassTimeSerializer(serializers.ModelSerializer):
    course_plan=UserCoursePlanSerializer()
    adjustclassmap_set=AdjustClassMapSerializer(many=True)
    change_teacher=serializers.StringRelatedField()
    teacher=serializers.StringRelatedField()
    cls=ClassSerializer()
    class Meta:
        model=AdjustClassTime
        fields='__all__'
#课表序列化器
class RollCallSerializer(serializers.ModelSerializer):
    act=AdjustClassTimeSerializer()
    def update(self, instance, validated_data):
        validated_data['state']=True
        validated_data['update_time']=timezone.now().strftime('%Y-%m-%d %H:%M:%S')
        return super(RollCallSerializer, self).update(instance,validated_data)

    class Meta:
        model=RollCall
        fields='__all__'

class AttendanceSerializer(serializers.ModelSerializer):
    total_studends_num=serializers.CharField()
    total_attendance_num=serializers.CharField()
    teaching_time__title=serializers.CharField()
    college__name=serializers.CharField()
    college_stage__name=serializers.CharField()
    class Meta:
        model=RollCall
        fields=('class_name','total_studends_num','total_attendance_num','teaching_time__title','college__name','college_stage__name')

